
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 11/01/2013 20:22:55
-- Generated from EDMX file: d:\dokumentumok\visual studio 2012\Projects\Tippelde\Tippelde.Entities\TippeldeModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [tippelde];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_CompetitorGroup_Competitor]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CompetitorGroup] DROP CONSTRAINT [FK_CompetitorGroup_Competitor];
GO
IF OBJECT_ID(N'[dbo].[FK_CompetitorGroup_Group]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CompetitorGroup] DROP CONSTRAINT [FK_CompetitorGroup_Group];
GO
IF OBJECT_ID(N'[dbo].[FK_BetCompetitor]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Bets] DROP CONSTRAINT [FK_BetCompetitor];
GO
IF OBJECT_ID(N'[dbo].[FK_GroupEvent_Group]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[GroupEvent] DROP CONSTRAINT [FK_GroupEvent_Group];
GO
IF OBJECT_ID(N'[dbo].[FK_GroupEvent_Event]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[GroupEvent] DROP CONSTRAINT [FK_GroupEvent_Event];
GO
IF OBJECT_ID(N'[dbo].[FK_EventDescriptorEvent]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Events] DROP CONSTRAINT [FK_EventDescriptorEvent];
GO
IF OBJECT_ID(N'[dbo].[FK_BetEvent]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Bets] DROP CONSTRAINT [FK_BetEvent];
GO
IF OBJECT_ID(N'[dbo].[FK_ResultEvent]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Events] DROP CONSTRAINT [FK_ResultEvent];
GO
IF OBJECT_ID(N'[dbo].[FK_CompetitorBetResult]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BetResults] DROP CONSTRAINT [FK_CompetitorBetResult];
GO
IF OBJECT_ID(N'[dbo].[FK_EventBetResult]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BetResults] DROP CONSTRAINT [FK_EventBetResult];
GO
IF OBJECT_ID(N'[dbo].[FK_BetBetResult]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Bets] DROP CONSTRAINT [FK_BetBetResult];
GO
IF OBJECT_ID(N'[dbo].[FK_EventDescriptorHomeTeam]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[EventDescriptors] DROP CONSTRAINT [FK_EventDescriptorHomeTeam];
GO
IF OBJECT_ID(N'[dbo].[FK_EventDescriptorAwayTeam]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[EventDescriptors] DROP CONSTRAINT [FK_EventDescriptorAwayTeam];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Competitors]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Competitors];
GO
IF OBJECT_ID(N'[dbo].[Bets]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Bets];
GO
IF OBJECT_ID(N'[dbo].[Events]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Events];
GO
IF OBJECT_ID(N'[dbo].[Groups]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Groups];
GO
IF OBJECT_ID(N'[dbo].[EventDescriptors]', 'U') IS NOT NULL
    DROP TABLE [dbo].[EventDescriptors];
GO
IF OBJECT_ID(N'[dbo].[Results]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Results];
GO
IF OBJECT_ID(N'[dbo].[BetResults]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BetResults];
GO
IF OBJECT_ID(N'[dbo].[Teams]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Teams];
GO
IF OBJECT_ID(N'[dbo].[CompetitorGroup]', 'U') IS NOT NULL
    DROP TABLE [dbo].[CompetitorGroup];
GO
IF OBJECT_ID(N'[dbo].[GroupEvent]', 'U') IS NOT NULL
    DROP TABLE [dbo].[GroupEvent];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Competitors'
CREATE TABLE [dbo].[Competitors] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [UserId] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'Bets'
CREATE TABLE [dbo].[Bets] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [HomeBet] int  NOT NULL,
    [AwayBet] int  NOT NULL,
    [BetAt] datetime  NOT NULL,
    [Checked] bit  NOT NULL,
    [Competitor_Id] int  NOT NULL,
    [Event_Id] int  NOT NULL,
    [BetResult_Id] int  NULL
);
GO

-- Creating table 'Events'
CREATE TABLE [dbo].[Events] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [EventDate] nvarchar(max)  NOT NULL,
    [BetStartAt] nvarchar(max)  NOT NULL,
    [BetEndAt] nvarchar(max)  NOT NULL,
    [Available] bit  NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [ShortName] nvarchar(max)  NOT NULL,
    [Status] int  NOT NULL,
    [EventDescriptor_Id] int  NOT NULL,
    [Result_Id] int  NULL
);
GO

-- Creating table 'Groups'
CREATE TABLE [dbo].[Groups] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [ShortName] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'EventDescriptors'
CREATE TABLE [dbo].[EventDescriptors] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NOT NULL,
    [EventInfo] nvarchar(max)  NOT NULL,
    [HomeTeam_Id] int  NOT NULL,
    [AwayTeam_Id] int  NOT NULL
);
GO

-- Creating table 'Results'
CREATE TABLE [dbo].[Results] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [HomeResult] int  NOT NULL,
    [AwayResult] int  NOT NULL,
    [AddedAt] datetime  NOT NULL
);
GO

-- Creating table 'BetResults'
CREATE TABLE [dbo].[BetResults] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [PointsEarned] int  NOT NULL,
    [CheckedAt] nvarchar(max)  NOT NULL,
    [Competitor_Id] int  NOT NULL,
    [Event_Id] int  NOT NULL
);
GO

-- Creating table 'Teams'
CREATE TABLE [dbo].[Teams] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [ShortName] nvarchar(max)  NOT NULL,
    [LogoUrl] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'CompetitorGroup'
CREATE TABLE [dbo].[CompetitorGroup] (
    [Competitors_Id] int  NOT NULL,
    [Groups_Id] int  NOT NULL
);
GO

-- Creating table 'GroupEvent'
CREATE TABLE [dbo].[GroupEvent] (
    [Groups_Id] int  NOT NULL,
    [Events_Id] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'Competitors'
ALTER TABLE [dbo].[Competitors]
ADD CONSTRAINT [PK_Competitors]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Bets'
ALTER TABLE [dbo].[Bets]
ADD CONSTRAINT [PK_Bets]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Events'
ALTER TABLE [dbo].[Events]
ADD CONSTRAINT [PK_Events]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Groups'
ALTER TABLE [dbo].[Groups]
ADD CONSTRAINT [PK_Groups]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'EventDescriptors'
ALTER TABLE [dbo].[EventDescriptors]
ADD CONSTRAINT [PK_EventDescriptors]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Results'
ALTER TABLE [dbo].[Results]
ADD CONSTRAINT [PK_Results]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'BetResults'
ALTER TABLE [dbo].[BetResults]
ADD CONSTRAINT [PK_BetResults]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Teams'
ALTER TABLE [dbo].[Teams]
ADD CONSTRAINT [PK_Teams]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Competitors_Id], [Groups_Id] in table 'CompetitorGroup'
ALTER TABLE [dbo].[CompetitorGroup]
ADD CONSTRAINT [PK_CompetitorGroup]
    PRIMARY KEY NONCLUSTERED ([Competitors_Id], [Groups_Id] ASC);
GO

-- Creating primary key on [Groups_Id], [Events_Id] in table 'GroupEvent'
ALTER TABLE [dbo].[GroupEvent]
ADD CONSTRAINT [PK_GroupEvent]
    PRIMARY KEY NONCLUSTERED ([Groups_Id], [Events_Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [Competitors_Id] in table 'CompetitorGroup'
ALTER TABLE [dbo].[CompetitorGroup]
ADD CONSTRAINT [FK_CompetitorGroup_Competitor]
    FOREIGN KEY ([Competitors_Id])
    REFERENCES [dbo].[Competitors]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Groups_Id] in table 'CompetitorGroup'
ALTER TABLE [dbo].[CompetitorGroup]
ADD CONSTRAINT [FK_CompetitorGroup_Group]
    FOREIGN KEY ([Groups_Id])
    REFERENCES [dbo].[Groups]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CompetitorGroup_Group'
CREATE INDEX [IX_FK_CompetitorGroup_Group]
ON [dbo].[CompetitorGroup]
    ([Groups_Id]);
GO

-- Creating foreign key on [Competitor_Id] in table 'Bets'
ALTER TABLE [dbo].[Bets]
ADD CONSTRAINT [FK_BetCompetitor]
    FOREIGN KEY ([Competitor_Id])
    REFERENCES [dbo].[Competitors]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_BetCompetitor'
CREATE INDEX [IX_FK_BetCompetitor]
ON [dbo].[Bets]
    ([Competitor_Id]);
GO

-- Creating foreign key on [Groups_Id] in table 'GroupEvent'
ALTER TABLE [dbo].[GroupEvent]
ADD CONSTRAINT [FK_GroupEvent_Group]
    FOREIGN KEY ([Groups_Id])
    REFERENCES [dbo].[Groups]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Events_Id] in table 'GroupEvent'
ALTER TABLE [dbo].[GroupEvent]
ADD CONSTRAINT [FK_GroupEvent_Event]
    FOREIGN KEY ([Events_Id])
    REFERENCES [dbo].[Events]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_GroupEvent_Event'
CREATE INDEX [IX_FK_GroupEvent_Event]
ON [dbo].[GroupEvent]
    ([Events_Id]);
GO

-- Creating foreign key on [EventDescriptor_Id] in table 'Events'
ALTER TABLE [dbo].[Events]
ADD CONSTRAINT [FK_EventDescriptorEvent]
    FOREIGN KEY ([EventDescriptor_Id])
    REFERENCES [dbo].[EventDescriptors]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_EventDescriptorEvent'
CREATE INDEX [IX_FK_EventDescriptorEvent]
ON [dbo].[Events]
    ([EventDescriptor_Id]);
GO

-- Creating foreign key on [Event_Id] in table 'Bets'
ALTER TABLE [dbo].[Bets]
ADD CONSTRAINT [FK_BetEvent]
    FOREIGN KEY ([Event_Id])
    REFERENCES [dbo].[Events]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_BetEvent'
CREATE INDEX [IX_FK_BetEvent]
ON [dbo].[Bets]
    ([Event_Id]);
GO

-- Creating foreign key on [Result_Id] in table 'Events'
ALTER TABLE [dbo].[Events]
ADD CONSTRAINT [FK_ResultEvent]
    FOREIGN KEY ([Result_Id])
    REFERENCES [dbo].[Results]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ResultEvent'
CREATE INDEX [IX_FK_ResultEvent]
ON [dbo].[Events]
    ([Result_Id]);
GO

-- Creating foreign key on [Competitor_Id] in table 'BetResults'
ALTER TABLE [dbo].[BetResults]
ADD CONSTRAINT [FK_CompetitorBetResult]
    FOREIGN KEY ([Competitor_Id])
    REFERENCES [dbo].[Competitors]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CompetitorBetResult'
CREATE INDEX [IX_FK_CompetitorBetResult]
ON [dbo].[BetResults]
    ([Competitor_Id]);
GO

-- Creating foreign key on [Event_Id] in table 'BetResults'
ALTER TABLE [dbo].[BetResults]
ADD CONSTRAINT [FK_EventBetResult]
    FOREIGN KEY ([Event_Id])
    REFERENCES [dbo].[Events]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_EventBetResult'
CREATE INDEX [IX_FK_EventBetResult]
ON [dbo].[BetResults]
    ([Event_Id]);
GO

-- Creating foreign key on [BetResult_Id] in table 'Bets'
ALTER TABLE [dbo].[Bets]
ADD CONSTRAINT [FK_BetBetResult]
    FOREIGN KEY ([BetResult_Id])
    REFERENCES [dbo].[BetResults]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_BetBetResult'
CREATE INDEX [IX_FK_BetBetResult]
ON [dbo].[Bets]
    ([BetResult_Id]);
GO

-- Creating foreign key on [HomeTeam_Id] in table 'EventDescriptors'
ALTER TABLE [dbo].[EventDescriptors]
ADD CONSTRAINT [FK_EventDescriptorHomeTeam]
    FOREIGN KEY ([HomeTeam_Id])
    REFERENCES [dbo].[Teams]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_EventDescriptorHomeTeam'
CREATE INDEX [IX_FK_EventDescriptorHomeTeam]
ON [dbo].[EventDescriptors]
    ([HomeTeam_Id]);
GO

-- Creating foreign key on [AwayTeam_Id] in table 'EventDescriptors'
ALTER TABLE [dbo].[EventDescriptors]
ADD CONSTRAINT [FK_EventDescriptorAwayTeam]
    FOREIGN KEY ([AwayTeam_Id])
    REFERENCES [dbo].[Teams]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_EventDescriptorAwayTeam'
CREATE INDEX [IX_FK_EventDescriptorAwayTeam]
ON [dbo].[EventDescriptors]
    ([AwayTeam_Id]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------